package com.zky.DAO.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.opensymphony.xwork2.ActionSupport;

import com.zky.DA0.UsersDao;
import com.zky.common.HibernateSessionFactory;
import com.zky.entity.Manager;
import com.zky.entity.Users;


public class UsersDaoImp extends ActionSupport implements UsersDao {
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public  List<Users> list = new ArrayList<Users>();




	
	public void delete(int id) {
		   Session session = HibernateSessionFactory.getSession();
		   Transaction transaction = session.beginTransaction();
		   try{
		    Users users = (Users)session.get(Users.class, id);
		    session.delete(users);
		    transaction.commit();
		   }catch(Exception e){
		    e.printStackTrace();
		   }
		}

		
	
	
		
		@SuppressWarnings("unchecked")
		public List<Users> findAll() {
		   Session session = HibernateSessionFactory.getSession();
		   Transaction transaction = session.beginTransaction();
		    list = new ArrayList<Users>();
		   try{
		    String hql = "from Users";
		    Query query=session.createQuery(hql);
		    list=query.list();
		    transaction.commit();
		   }catch(Exception e){
		    e.printStackTrace();
		   }finally{
		    session.close();
		   }
		   return list;
		}

		
		
		
		public Users findById(int id) {
		   Session session = HibernateSessionFactory.getSession();
		   Transaction transaction = session.beginTransaction();
		   try{
			   Users users = (Users)session.createQuery("from Users s where s.id='"+id+"'").uniqueResult();
		    //Student student=(Student) session.get(Student.class, id);
		    transaction.commit();
		    return users;
		   }catch(Exception e){
		    e.printStackTrace();
		    transaction.rollback();
		   }finally{
		    session.close();
		   }
		   return null;
		}

		
	
		
		
		public void save(Users users) {
		   Session session = HibernateSessionFactory.getSession();
		   Transaction transaction = session.beginTransaction();
		   try{
		    session.save(users);
		    transaction.commit();
		   }catch(Exception e){
		    e.printStackTrace();
		   }finally{
		    session.close();
		   }
		}

		
		
	
		
		public void update(Users users) {
		   Session session = HibernateSessionFactory.getSession();
		   Transaction transaction = session.beginTransaction();
		   try{			   
		    session.update(users);
		    transaction.commit();
		   }catch(Exception e){
		    e.printStackTrace();
		   }finally{
		    session.close();
		   }

		}




		@Override
		public List<Users> findAll1(String name, String psw) {
			 Session session = HibernateSessionFactory.getSession();
			 Transaction transaction = session.beginTransaction();		
			  try{
				  String hql="from Users as muser where muser.username=? and muser.password=?";
				  Query query=session.createQuery(hql);
				  query.setString(0, name);
				  query.setString(1, psw);
				   list=query.list();
					if (null == list || list.size() == 0) {
				    	System.out.print("没有");
				    }
					else{
						System.out.print("正确");
					}
				   transaction.commit();
				   
				   }catch(Exception e){
				    e.printStackTrace();
				   }finally{
				    session.close();
			
		}
			return list;
		}
		public List<Users> findAll2(String name) {
			 Session session = HibernateSessionFactory.getSession();
			 Transaction transaction = session.beginTransaction();		
			  try{
				  String hql="from Users as muser where muser.username=?";
				  Query query=session.createQuery(hql);
				  query.setString(0, name);
				   list=query.list();
					if (null == list || list.size() == 0) {
				    	System.out.print("没有");
				    }
					else{
						System.out.print("正确");
					}
				   transaction.commit();
				   
				   }catch(Exception e){
				    e.printStackTrace();
				   }finally{
				    session.close();
			
		}
			return list;
		}
		
		public List<Users> findAll4(String name) {
			 Session session = HibernateSessionFactory.getSession();
			 Transaction transaction = session.beginTransaction();		
			  try{
				  String hql="from Users as muser where muser.name=?";
				  Query query=session.createQuery(hql);
				  query.setString(0, name);
				   list=query.list();
					if (null == list || list.size() == 0) {
				    	System.out.print("没有");
				    }
					else{
						System.out.print("正确");
					}
				   transaction.commit();
				   
				   }catch(Exception e){
				    e.printStackTrace();
				   }finally{
				    session.close();
			
		}
			return list;
		}
		

}
